//
//  PS_RegisterViewController.m
//  PolicySystem
//
//  Created by 丹丹 on 2018/10/24.
//  Copyright © 2018年 xuezi. All rights reserved.
//

#import "PS_RegisterViewController.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+Add.h"
#import "ZDXTabBarControllerViewController.h"
//#import "JPUSHService.h"
//#import "PS_WebViewController.h"
@interface PS_RegisterViewController (){
    
    BOOL isAggree;
}
@property (strong, nonatomic) IBOutlet UIScrollView *backView;
@property (strong, nonatomic) IBOutlet UIView *height_ContantView;
@property (strong, nonatomic) IBOutlet UITextField *phoneTF;
@property (strong, nonatomic) IBOutlet UITextField *codeTF;

@property (strong, nonatomic) IBOutlet UITextField *passwordTF;
@property (strong, nonatomic) IBOutlet UITextField *inviteTF;

@property (strong, nonatomic) IBOutlet UIButton *protocolButton;

@property (strong, nonatomic) IBOutlet UILabel *width_haveLoginLabel;
@property (strong, nonatomic) IBOutlet UIButton *getCodeButton;

@property (strong, nonatomic) IBOutlet UIView *codeView;
@property (strong, nonatomic) IBOutlet UILabel *sencondLabel;
@end

@implementation PS_RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.nav_btmView.backgroundColor =[UIColor clearColor];
    self.statusBarView.hidden = YES;
    self.nav_backImg.image  = [UIImage imageNamed:@"w_back"];
    
    _getCodeButton.layer.borderWidth = 0.5;
    _getCodeButton.layer.borderColor = [UIColor whiteColor].CGColor;//238 114 111
    _getCodeButton.layer.cornerRadius = 10;
    
    _codeView.layer.borderWidth = 0.5;
    _codeView.layer.borderColor = [UIColor whiteColor].CGColor;
    _codeView.layer.cornerRadius = 10;
    
    _phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    _codeTF.keyboardType = UIKeyboardTypeNumberPad;
    [_phoneTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_codeTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_passwordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_inviteTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    //设置placeholder为白色
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:_phoneTF.placeholder];
    [placeholder addAttribute:NSForegroundColorAttributeName
                        value:[UIColor whiteColor]
                        range:NSMakeRange(0, placeholder.length)];
    _phoneTF.attributedPlaceholder = placeholder;
    
    NSMutableAttributedString *placeholder2 = [[NSMutableAttributedString alloc] initWithString:_passwordTF.placeholder];
    [placeholder2 addAttribute:NSForegroundColorAttributeName
                         value:[UIColor whiteColor]
                         range:NSMakeRange(0, placeholder2.length)];
    _passwordTF.attributedPlaceholder = placeholder2;
    
    NSMutableAttributedString *placeholder3 = [[NSMutableAttributedString alloc] initWithString:_codeTF.placeholder];
    [placeholder3 addAttribute:NSForegroundColorAttributeName
                         value:[UIColor whiteColor]
                         range:NSMakeRange(0, placeholder3.length)];
    _codeTF.attributedPlaceholder = placeholder3;
    
    
    
//    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@" 我已阅读并同意《注册许可协议》"];
//    NSRange range1 = [[str string] rangeOfString:@" 我已阅读并同意"];
//    [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(151, 151, 151) range:range1];
//    NSRange range2 = [[str string] rangeOfString:@"《注册许可协议》"];
//    [str addAttribute:NSForegroundColorAttributeName value:RGBCOLOR(238, 114, 111) range:range2];
//    [_protocolButton setAttributedTitle:str forState:UIControlStateNormal];
    
    // Do any additional setup after loading the view from its nib.
}

- (IBAction)getCodeAction:(id)sender {
    
    if (_phoneTF.text.length == 0) {
        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];
        return;
    }
    
//    [Utils post:YANZHENGMA parameters:@{@"mobile":_phoneTF.text} success:^(NSInteger recode, id jsonObject) {
//
//        [self startAnimation];
//
//    } failure:^(NSInteger recode, NSError *error) {
//
//    }];
    
}

- (IBAction)rigisterAction:(id)sender {
    
    if (_phoneTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入手机号码" toView:self.view];
        
        return;
    }
    
    if (_phoneTF.text.length < 11) {
        
        [MBProgressHUD showError:@"请输入h正确手机号码" toView:self.view];
        
        return;
    }
    if (_codeTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入验证码" toView:self.view];
        
        return;
    }
    if (_passwordTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入密码" toView:self.view];
        
        return;
    }
    
    if (_passwordTF.text.length < 6 || _passwordTF.text.length > 18) {
        
        [MBProgressHUD showError:@"请输入正确密码" toView:self.view];
        
        return;
    }
    if (_inviteTF.text.length == 0) {
        
        [MBProgressHUD showError:@"请输入邀请码" toView:self.view];
        
        return;
    }
    
    if (!isAggree) {
        
        [MBProgressHUD showError:@"请先同意《注册许可协议》" toView:self.view];
        
        return;
    }
    
    [Utils post:REGISTER parameters:@{@"mobile":_phoneTF.text,@"code":_codeTF.text,@"password":_passwordTF.text,@"invite_code":_inviteTF.text}  success:^(NSInteger recode, id jsonObject) {
        
        [MBProgressHUD showMessag:[jsonObject objectForKey:@"msg"] toView:self.view];
        
//        [MobClick event:@"Rigister"];
        
        [self goLogin];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}

- (IBAction)goProtocol:(id)sender {
    
  
    
    //http://active.ztcfb.com/baodanxitong/registerprotocol.html?type=0
    
//    PS_WebViewController * VC = [[PS_WebViewController alloc]init];
//    VC.webUrl = [NSString stringWithFormat:@"http://39.107.247.20:10017/registerprotocol.html?type=0"];
//    VC.isFrom = 2;
//    [self.navigationController pushViewController:VC animated:YES];
    
}
- (IBAction)agreeProtocal:(id)sender {
    
    isAggree = !isAggree;
    
    if (isAggree) {
        
        [_protocolButton setImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateNormal];
    }else{
        
        [_protocolButton setImage:[UIImage imageNamed:@"xuanzhong_nor"] forState:UIControlStateNormal];
    }
    
}

- (void)goLogin{
    
    [Utils post:LOGIN parameters:@{@"mobile":_phoneTF.text,@"password":_passwordTF.text}  success:^(NSInteger recode, id jsonObject) {
        
        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        if (dataDic.count > 0) {
            
            [Utils starageData:dataDic];
            
            NSString * userid = [NSString stringWithFormat:@"%@",[dataDic objectForKey:@"id"]];
            
//            [JPUSHService setAlias:userid completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
//                
//            } seq:1];
            
        }
        [MBProgressHUD showMessag:[jsonObject objectForKey:@"msg"] toView:self.view];
        
        [self crearTabBarController];
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
-(void)crearTabBarController{
    
    ZDXTabBarControllerViewController * tab = [[ZDXTabBarControllerViewController alloc]init];
    UIWindow * mainWindow = [UIApplication sharedApplication].keyWindow;
    mainWindow.rootViewController =tab;
    //    [mainWindow makeKeyAndVisible];
}
- (IBAction)goLoginAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)textFieldDidChange:(UITextField *)textField

{
    
    if (textField == _phoneTF) {
        
        if (textField.text.length > 11) {
            
            textField.text = [textField.text substringToIndex:11];
            
        }
        
        if (_phoneTF.text.length>10) {
        
            [Utils post:@"/api/index/checkmobile.html"parameters:@{@"mobile":_phoneTF.text}  success:^(NSInteger recode, id jsonObject) {
                
            } failure:^(NSInteger recode, NSError *error) {
                
            }];
        }
        
    }
    
    if (textField == _codeTF) {
        
        if (textField.text.length > 8) {
            
            textField.text = [textField.text substringToIndex:8];
            
        }
    }
    
    if (textField == _passwordTF) {
        
        if (textField.text.length > 18) {
            
            textField.text = [textField.text substringToIndex:18];
            
        }
    }
    if (textField == _inviteTF) {
        
        if (textField.text.length > 6) {
            
            textField.text = [textField.text substringToIndex:6];
            
        }
    }
    
    
    
}

- (void)startAnimation
{
    __block NSInteger time = 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(time <= 0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self->_codeView.hidden = YES;
                self->_getCodeButton.hidden = NO;
                [self->_getCodeButton setTitle:[NSString stringWithFormat:@"获取验证码"] forState:UIControlStateNormal];
                self->_getCodeButton.layer.borderColor = [UIColor whiteColor].CGColor;
                [self->_getCodeButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            });
            
        }else{
            
            int seconds = time % 60;
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                self->_codeView.hidden = NO;
                self->_getCodeButton.hidden = YES;
                
                self->_sencondLabel.text = [NSString stringWithFormat:@"%d",seconds];
            });
            time--;
        }
    });
    dispatch_resume(_timer);
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
